package View;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.text.NumberFormat;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import objects.Gene;
import objects.Treatment;

import Control.Controller;
import Control.InitializeData;
import Control.VisualizationSettings;

/**
 * 
 * Holds all of the GUI Properties that are apart of the GeneVisualization
 * JFrame. These properties include but are not limited to: - Selecting the Gene
 * - Selecting which treatments to see - Selecting which way to visualize data +
 * Start Position + Cigar String (mark where the gene read occurs) + Collapsed
 * View + Subset 0 Only - Grid Data
 * 
 * @author stephenbwilliamson
 */
public class VisualizationPropertiesPanel extends JPanel {

	private static final long serialVersionUID = -3057832432110020230L;
	private static VisualizationPropertiesPanel instance = null;

	protected VisualizationPropertiesPanel() {
	}

	public static VisualizationPropertiesPanel getInstance() {
		if (instance == null) {
			instance = new VisualizationPropertiesPanel();
		}
		return instance;
	}

	private JLabel jlActiveGene;
	private JLabel jlParent = new JLabel("Parent:", JLabel.RIGHT);
	private JLabel jlStartPosition = new JLabel("Start:", JLabel.RIGHT);
	private JLabel jlEndPosition = new JLabel("End:", JLabel.RIGHT);
	private JLabel jlStartPosNum = new JLabel("");
	private JLabel jlEndPosNum = new JLabel("");
	private JLabel jlParentName = new JLabel("");
	private JLabel jlTreatmentHeader = new JLabel("Choose Treatments to be Displayed");
	private JComboBox jcbGene = new JComboBox();
	private JLabel jlDisplay = new JLabel("Display Configuration:");
	private JLabel jlGridSettings = new JLabel("Grid Settings:");
	private JLabel jlGridLabel = new JLabel("Number of Positions:");
	private JRadioButton jrbDisplayAllSubsets = new JRadioButton(
			"Display All Subsets", true);
	private JRadioButton jrbCollapseSubsets = new JRadioButton(
			"Collapse Subsets");
	private JTextField jtfGridSpace = new JTextField(10);
	private JButton jbGridUpdate = new JButton(">");

	private JPanel jpTreatments = new JPanel();
	private JPanel treatmentList = new JPanel();

	public JPanel initPanel() {		
		// Font Creation
		Font h1 = new Font("Helvetica", Font.PLAIN, 32);
		Font h2 = new Font("Helvetica", Font.PLAIN, 24);
		Font h3 = new Font("Helvetica", Font.PLAIN, 18);
		
		
		JLabel jlHeader = new JLabel("Visualization Properties");
		jlHeader.setFont(h1);
		
		jlActiveGene = new JLabel("Active Gene:");
		jlActiveGene.setFont(h3);
		jlActiveGene.setForeground(Color.RED);
		
		jlTreatmentHeader.setFont(h2);
		jlDisplay.setFont(h2);
		jlGridSettings.setFont(h2);

		// Create the Gene ComboBox
		String[] geneList = InitializeData.getInstance().getGeneList();
		jcbGene = new JComboBox(geneList);
		jcbGene.setEditable(true);
		jcbGene.setSelectedIndex(0);
		jcbGene.addActionListener(new VisualizationSettings());

		// Get rid of the currently non-used portions
		switchTreatmentVisbility(false);

		// Create the Button Group (Display Configuration)
		jrbDisplayAllSubsets.addActionListener(new VisualizationSettings());
		jrbCollapseSubsets.addActionListener(new VisualizationSettings());
		ButtonGroup group = new ButtonGroup();
		group.add(jrbDisplayAllSubsets);
		group.add(jrbCollapseSubsets);

		this.setPreferredSize(new Dimension(400, getHeight()));
		setSize(400, getHeight());
		this.setBackground(new Color(250, 250, 250));
		
		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		layout.setHorizontalGroup(layout
				.createSequentialGroup()
				.addGap(12)
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.LEADING)
								.addComponent(jlHeader)
								.addGroup(
										layout.createSequentialGroup()
												.addComponent(jlActiveGene)
												.addComponent(jcbGene))
								.addGroup(
										layout.createSequentialGroup()
												.addGap(12)
												.addGroup(
														layout.createParallelGroup(
																GroupLayout.Alignment.TRAILING)
																.addComponent(
																		jlParent)
																.addComponent(
																		jlStartPosition)
																.addComponent(
																		jlEndPosition))
												.addGroup(
														layout.createParallelGroup(
																GroupLayout.Alignment.LEADING)
																.addComponent(
																		jlParentName)
																.addComponent(
																		jlStartPosNum)
																.addComponent(
																		jlEndPosNum)))
								.addComponent(jpTreatments)
								.addComponent(jlDisplay)
								.addGroup(
										layout.createSequentialGroup()
												.addGap(12)
												.addComponent(
														jrbDisplayAllSubsets))
								.addGroup(
										layout.createSequentialGroup()
												.addGap(12)
												.addComponent(
														jrbCollapseSubsets))
								.addComponent(jlGridSettings)
								.addGroup(
										layout.createSequentialGroup()
												.addGap(12)
												.addComponent(jlGridLabel)
												.addComponent(jtfGridSpace, 48,
														48, 64)
												.addComponent(jbGridUpdate, 24,
														24, 48))));

		layout.setVerticalGroup(layout
				.createSequentialGroup()
				.addGap(48)
				.addComponent(jlHeader)
				.addGap(16)
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.BASELINE)
								.addComponent(jlActiveGene)
								.addComponent(jcbGene))
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.BASELINE)
								.addComponent(jlParent)
								.addComponent(jlParentName))
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.BASELINE)
								.addComponent(jlStartPosition)
								.addComponent(jlStartPosNum))
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.BASELINE)
								.addComponent(jlEndPosition)
								.addComponent(jlEndPosNum))
				.addGap(16)
				.addComponent(jpTreatments)
				.addGap(16)
				.addComponent(jlDisplay)
				.addComponent(jrbDisplayAllSubsets)
				.addComponent(jrbCollapseSubsets)
				.addGap(16)
				.addComponent(jlGridSettings)
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.BASELINE)
								.addComponent(jlGridLabel)
								.addComponent(jtfGridSpace)
								.addComponent(jbGridUpdate)));
		this.validate();
		return this;
	}

	private void switchTreatmentVisbility(boolean bool) {
		jlParent.setVisible(bool);
		jlParentName.setVisible(bool);
		jlStartPosition.setVisible(bool);
		jlEndPosition.setVisible(bool);
		jlStartPosNum.setVisible(bool);
		jlEndPosNum.setVisible(bool);
		jpTreatments.setVisible(bool);
	}

	public void loadTreatments() {
		Gene activeGene = Controller.getInstance().getCurrentGene();
		if (activeGene == null) {
			switchTreatmentVisbility(false);
			return;
		}
		// Set all Portions as Visible Now
		switchTreatmentVisbility(true);

		// Add the Information to the List
		jlStartPosNum.setText(NumberFormat.getInstance().format(
				activeGene.getStartPosition()));
		jlEndPosNum.setText(NumberFormat.getInstance().format(
				activeGene.getEndPosition()));
		jlParentName.setText(activeGene.getParentChromosome().toString());

		// Load the Treatment List
		getTreatmentList();
	}

	private void getTreatmentList() {
		jpTreatments.removeAll();
		jpTreatments.setLayout(new BoxLayout(jpTreatments, BoxLayout.Y_AXIS));
		jpTreatments.setBackground(new Color(200, 200, 200));
		jpTreatments.add(jlTreatmentHeader);


		treatmentList.removeAll();
		treatmentList.setLayout(new BoxLayout(treatmentList, BoxLayout.Y_AXIS));
		treatmentList.setBackground(new Color(200, 200, 200));
		treatmentList.setBorder(BorderFactory.createEmptyBorder(4, 12, 0, 0));

		ArrayList<Treatment> treatments = (ArrayList<Treatment>) Controller
				.getInstance().getCurrentGene().getTreatmentList();
		for (Treatment t : treatments) {
			JCheckBox jcb = new JCheckBox("Treatment " + t.getTreatmentID());
			if (!t.hasColor())
				t.setColor(Controller.getInstance().getRandomColor());
			jcb.setForeground(t.getColor());
			jcb.addItemListener(new VisualizationSettings());
			treatmentList.add(jcb);
		}

		treatmentList.validate();
		jpTreatments.add(treatmentList);
		jpTreatments.validate();
	}
}
